package com.hymjweb.demo.hymj.system_maintenance.user.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import com.hymjweb.demo.hymj.system_maintenance.user.service.XtUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/pages/user_manage")
public class XtUserController extends BaseController {

    @Autowired
    private XtUserService xtUserService;

    @RequestMapping("/findCacheAll")
    @ResponseBody
    public Object findCacheAll(@RequestParam(required = false) Map param) {
        return xtUserService.findCacheAll(param);
    }

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/user_manage/xtuser";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) {
        return xtUserService.getPageList(param);
    }

    @RequestMapping("/fecthLoginUserPojo")
    @ResponseBody
    public XtUserPojo fecthLoginUserPojo() throws Exception {
        return xtUserService.queryLoginUserPojo();
    }

    @RequestMapping("/queryXtUserPojoByMobile")
    @ResponseBody
    public BaseResultMap query(@RequestParam(required = false) String mobile) throws Exception {
        if (DataTypeUtil.isEmptyStr(mobile)) {
            throwException("根据号码检索用户对象参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("电话号码【" + mobile + "】已经有用户在使用，不能使用该号码创建新的用户!");

        XtUserPojo pojo = xtUserService.findUK(mobile);
        if (pojo == null) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("电话号码【" + mobile + "】检测通过，可以使用!");
        }
        return baseResultMap;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(@RequestBody XtUserPojo xtUserPojo, String opFlag) throws Exception {
        if (xtUserPojo == null || DataTypeUtil.isEmptyStr(opFlag)) {
            throwException("用户保存参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("用户保存成功!");

        int affectRows = 0;
        if (opFlag.equals(SysConstant.OPER_ADD)) {
            affectRows = xtUserService.insert(xtUserPojo);
        } else {
            affectRows = xtUserService.update(xtUserPojo);
        }

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("用户保存失败，请重试");
        }

        return baseResultMap;
    }

    @RequestMapping("/resetPassword")
    @ResponseBody
    public Object resetPassword(@RequestBody Map map) throws Exception {
        if (map == null || map.isEmpty()) {
            throwException("密码修改参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("登录密码修改成功!");


        String pwd1 = DataTypeUtil.toStringObject(map.get("pwd1"));
        String pwd2 = DataTypeUtil.toStringObject(map.get("pwd2"));
        if (DataTypeUtil.isEmptyStr(pwd1)) {
            throwException("密码修改参数错误");
        }
        if (DataTypeUtil.isEmptyStr(pwd2)) {
            throwException("密码修改参数错误");
        }

        if (!pwd1.equals(pwd2)) {
            throwException("两次输入的密码不相同");
        }

        int affectRows = xtUserService.resetPassword(pwd1);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("登录密码修改失败，请重试!");
        }
        return baseResultMap;
    }

    @RequestMapping("/insert")
    @ResponseBody
    @Deprecated
    public Object insertAndUpload(@RequestBody XtUserPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("添加用户参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("添加用户成功!");

        int affectRows = xtUserService.insert(pojo);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("添加用户失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/update")
    @ResponseBody
    @Deprecated
    public Object update(@RequestBody XtUserPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("更新用户参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("更新用户成功!");

        int affectRows = xtUserService.update(pojo);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("更新用户失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) Object id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("删除用户参数错误!");
        }
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("删除用户成功!");
        int affectRows = xtUserService.delete(DataTypeUtil.toNotNullString(id));
        if (affectRows != 0) {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("删除用户失败!");
        }

        return baseResultMap;
    }

}
